# -*- coding: UTF-8 -*-
# @Date ：2024/6/24 12:06
# @File：log_config.py
# @Author：mmmme
# @IDE：PyCharm

__all__ = ['getLogger', 'initLogConf']

import logging
import logging.config
import os


def genLogDict(DIR_DEBUG, DIR_INFO, DIR_ERROR, log_level):
    LOGGING = {
        "version": 1,
        "disable_existing_loggers": False,
        # 日志输出格式设置
        "formatters": {
            "simple": {
                'format': '%(asctime)s [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s'
            },
            'standard': {
                'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s'
            },
        },

        "handlers": {
            # 控制台处理器,控制台输出
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "simple",
                "stream": "ext://sys.stdout"
            },
            # 默认处理器, 文件大小滚动
            "debug": {
                "class": "logging.handlers.TimedRotatingFileHandler",
                "level": "DEBUG",
                "formatter": "simple",
                'filename': os.path.join(DIR_DEBUG, 'debug.log'),
                # "maxBytes": 1024 * 1024 * 10,  # 8 MB
                # 'when' = 'MIDNIGHT',#是指过了午夜12点，就会创建新的日志
                'when': 'H',  # 单位： 小时
                'interval': 1,  # 滚动周期
                "backupCount": 48,  # 备份48个小时
                "encoding": "utf8"
            },
            # 错误日志, 文件大小滚动
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': os.path.join(DIR_ERROR, 'error.log'),
                # 'maxBytes': 1024 * 1024 * 10,
                'when': 'H',  # 单位： 小时
                'interval': 1,  # 滚动周期
                'backupCount': 10,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # info 及以上级别, 日期滚动
            'info': {
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'level': 'INFO',
                "formatter": "simple",
                'filename': os.path.join(DIR_INFO, 'info.log'),
                # 'maxBytes': 1024 * 1024 * 10,
                'when': 'H',  # 单位： 小时
                'interval': 1,  # 滚动周期
                'backupCount': 48,  # 备份48小时
                "encoding": "utf8"
            },
        },
        'loggers': {
            'websockets': {
                'handlers': ['debug', 'error', 'info', 'console'],
                'level': "ERROR",
                'propagate': False
            },
            'asyncio': {
                'handlers': ['debug', 'error', 'info', 'console'],
                'level': "ERROR",
                'propagate': False
            },
            'comtypes': {
                'handlers': ['debug', 'error', 'info', 'console'],
                'level': "INFO",
                'propagate': False
            },
        },
        "root": {
            'handlers': ['debug', 'error', 'info', 'console'],
            'level': log_level,
            'propagate': False
        }
    }
    return LOGGING


def initLogConf(base_path, log_level):
    """
    配置日志
    """

    logDir = os.path.join(base_path, "logs")
    if not os.path.exists(logDir):
        os.makedirs(logDir)  # 创建路径
    DIR_DEBUG = os.path.join(base_path, 'logs', 'debug')
    DIR_INFO = os.path.join(base_path, 'logs', 'info')
    DIR_ERROR = os.path.join(base_path, 'logs', 'error')

    os.makedirs(DIR_DEBUG, exist_ok=True)
    os.makedirs(DIR_INFO, exist_ok=True)
    os.makedirs(DIR_ERROR, exist_ok=True)
    logDict = genLogDict(DIR_DEBUG, DIR_INFO, DIR_ERROR, log_level)
    logging.config.dictConfig(logDict)
    logging.info("Init logging")


def getLogger(name):
    return logging.getLogger(name)
